//
//  PanelInverterEditNavigationView.swift
//  GreenDeal
//
//  Created by Moennig Q on 22/12/2022.
//

import UIKit
import SnapKit
class GreenNaviBarView: UIView {
    let topBackView = UIView()
    let backToPreBtn = UIButton()
    let titleBackView  = UIView()
    let titleLabel = ContentLabel(R.font.interBold(size: 18),hexStr: "#FFFFFFFF")
    let subTitieLabel = ContentLabel(R.font.interMedium(size: 12),hexStr: "#FFFFFFB3")
    let topBackImageViewBottomMargin: CGFloat = -20
    //IB init
    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
    }
    //纯代码创建
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initNaviBar()
        self.setUpView()
    }
    //
    init(topBGImageName: String = "", customBackToPreFlag: Bool = false) {
        super.init(frame: CGRect.zero)
        self.initNaviBar(topBGImageName, customBackToPreFlag: customBackToPreFlag)
        self.setUpView()
    }
    //true：表示定制自己的backToPreBtn.addTarget操作，不用默认的
    private func initNaviBar(_ topBGImageName: String = "", customBackToPreFlag: Bool = false) {
        self.backgroundColor = .white
        topBackView.backgroundColor = UIColor(hexStr: Constant.ThemeColorHexStr)
        self.addSubview(topBackView)
        topBackView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
        }
        //
        let backToPreBtnTopMargin: CGFloat = UIDevice.getStatusBarHeight() + 10
        backToPreBtn.setImage(R.image.navigation_bar_back_white_icon(), for: UIControl.State.normal)
        topBackView.addSubview(backToPreBtn)
        backToPreBtn.snp.makeConstraints { make in
            make.left.equalTo(8)
            make.top.equalTo(backToPreBtnTopMargin)
            make.width.height.equalTo(40)
            make.bottom.equalTo(-30)
        }
        //titleBackView
        topBackView.addSubview(titleBackView)
        titleBackView.snp.makeConstraints { make in
            make.left.greaterThanOrEqualTo(backToPreBtn.snp.right)
            make.right.lessThanOrEqualToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.centerY.equalTo(backToPreBtn)
            make.centerX.equalToSuperview()
        }
        //titleLabel
        titleLabel.numberOfLines = 2
        titleLabel.textAlignment = .center
        titleLabel.lineBreakMode = .byTruncatingTail
        titleBackView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.width.greaterThanOrEqualTo(0)
        }
        //subtitle
        subTitieLabel.textAlignment = .center
        titleBackView.addSubview(subTitieLabel)
        subTitieLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(3)
            make.height.greaterThanOrEqualTo(0)
            make.left.right.equalTo(titleLabel)
            make.bottom.equalToSuperview()
        }
        //
        if let image = UIImage(named: topBGImageName){
            let topBackImageView  = UIImageView(image: image)
            topBackImageView.contentMode = .scaleAspectFit
            topBackView.addSubview(topBackImageView)
            topBackImageView.snp.makeConstraints { make in
                make.top.equalToSuperview()
                make.height.equalToSuperview()
                make.width.greaterThanOrEqualTo(0)
                make.right.equalToSuperview()
            }
            topBackView.sendSubviewToBack(topBackImageView)
        }
        if customBackToPreFlag == false{//使用默认的
            backToPreBtn.addTarget(self, action: #selector(backToPreViewController), for: UIControl.Event.touchUpInside)
        }
    }
    //MARK: - 退回到一步
    @objc func backToPreViewController(){
        self.endEditing(true)
        if let naviVC = self.firstNaviViewController(){
            naviVC.popViewController(animated: false)
        }
    }
    func setUpView() {
        
    }
    func setTitleInfo(_ title: String, subTitle: String = ""){
        self.titleLabel.text = title
        self.subTitieLabel.text = subTitle
    }
}
